package com.freesky.biz.rights.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freesky.biz.rights.domain.RightsQuesEntity;
import com.freesky.biz.rights.domain.RightsQuesRecordEntity;
import com.freesky.biz.rights.domain.RightsThirdCardEntity;
import com.freesky.biz.rights.service.IRightsQuesRecordService;
import com.freesky.biz.rights.service.IRightsQuesService;
import com.freesky.biz.rights.service.IRightsThirdCardService;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 第三方卡券管理
 */
@RestController
@RequestMapping("/rights/quesrecord")
public class RightsQuesRecordController extends BaseController {

    @Autowired
    private IRightsQuesRecordService rightsQuesRecordService;
    @Autowired
    private IRightsQuesService rightsQuesService;
    /**
     * 获取卡券列表
     */
    @PreAuthorize("@ss.hasPermi('rights:quesrecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(RightsQuesRecordEntity rightsQuesRecord) {
        LambdaQueryWrapper<RightsQuesRecordEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(RightsQuesRecordEntity::getCreateTime);
        wrapper.eq(RightsQuesRecordEntity::getDelFlag, "0");
        if(StringUtils.isNotEmpty(rightsQuesRecord.getBusiName())){
            wrapper.like(RightsQuesRecordEntity::getBusiName, rightsQuesRecord.getBusiName());
        }
        if(StringUtils.isNotEmpty(rightsQuesRecord.getBusiId())){
            wrapper.like(RightsQuesRecordEntity::getBusiId, rightsQuesRecord.getBusiId());
        }

        if(StringUtils.isNotEmpty(rightsQuesRecord.getCustId())){
            wrapper.like(RightsQuesRecordEntity::getCustId, rightsQuesRecord.getCustId());
        }

        if(StringUtils.isNotEmpty(rightsQuesRecord.getMobile())){
            wrapper.like(RightsQuesRecordEntity::getMobile, rightsQuesRecord.getMobile());
        }

        if(StringUtils.isNotEmpty(rightsQuesRecord.getAccordFlag())){
            wrapper.eq(RightsQuesRecordEntity::getAccordFlag, rightsQuesRecord.getAccordFlag());
        }

        IPage<RightsQuesRecordEntity> list = rightsQuesRecordService.page(plusPage(), wrapper);
        return getDataTable(list);
    }

    /**
     * 根据id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('rights:quesrecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        Map<String, Object> respMap = new HashMap<>();
        Set<String> quesIdList = new HashSet<>();
        RightsQuesRecordEntity quesRecordEntity = rightsQuesRecordService.getById(id);
        if(rightsQuesRecordService !=null){
            JSONArray answerDetailArray = JSON.parseArray(quesRecordEntity.getAnswerDetail());
            for(int i=0;i<answerDetailArray.size();i++){
                JSONObject jsonObject = answerDetailArray.getJSONObject(i);
                quesIdList.add(jsonObject.getString("quesId"));
            }
            if(quesIdList.size() > 0){
                List<RightsQuesEntity> quesList = rightsQuesService.selectQuesListByIds(quesIdList);
                respMap.put("quesList",quesList);
            }
        }
        respMap.put("quesRecord",quesRecordEntity);
        return success(respMap);
    }
}
